package com.sen.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sen.mapper.BmsPostTagMapper;
import com.sen.model.entity.BmsPostTag;
import com.sen.model.entity.BmsTag;
import com.sen.service.BmsPostTagService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Set;

/**
 * <p>
 * 话题-标签 中间表 服务实现类
 * </p>
 *
 * @author ergou
 * @since 2021-04-12
 */
@Service
public class BmsPostTagServiceImpl extends ServiceImpl<BmsPostTagMapper, BmsPostTag> implements BmsPostTagService {

    @Override
    public List<BmsPostTag> selectByTopicId(String topicId) {
        QueryWrapper<BmsPostTag> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(BmsPostTag::getTopicId,topicId);
         return this.baseMapper.selectList(wrapper);
    }

    @Override
    public void createTopicTag(String id, List<BmsTag> tags) {
        //先删除topicId对应的所有记录
        this.baseMapper.delete(new LambdaQueryWrapper<BmsPostTag>().eq(BmsPostTag::getTopicId,id));

        // 循环保存对应关联
        tags.forEach(tag -> {
            BmsPostTag bmsPostTag = new BmsPostTag();
            bmsPostTag.setTopicId(id);
            bmsPostTag.setTagId(tag.getId());
            this.baseMapper.insert(bmsPostTag);
        });
    }

    @Override
    public Set<String> selectTopicIdsByTagId(String id) {
        return this.baseMapper.getTopicIdsByTagId(id);
    }
}
